package xyz.labmem.app.main

import javafx.scene.Cursor
import javafx.scene.image.Image
import javafx.scene.layout.Background
import javafx.scene.layout.Pane
import javafx.stage.StageStyle
import xyz.labmem.app.config.AppConfig
import xyz.labmem.app.config.AppConfig.mouseImage
import xyz.labmem.app.dialog.ConfirmDialog
import xyz.labmem.app.dialog.FormDialog
import xyz.labmem.app.dialog.FormInterFace
import xyz.labmem.app.loading.LoadingStage
import xyz.labmem.app.task.ClientAutoRunTask
import xyz.labmem.app.tray.AppTray
import xyz.labmem.app.unit.BaseStage
import xyz.labmem.app.unit.ProxyListUnit
import xyz.labmem.app.unit.TopDrag
import xyz.labmem.app.util.BackgroundMusic
import xyz.labmem.app.util.ScreenType
import xyz.labmem.lipx.core.client.core.LabSSHPenetrationClient
import xyz.labmem.lipx.core.context.AppContext
import xyz.labmem.lipx.core.context.AppContext.Client.parallelPool
import xyz.labmem.lipx.core.tool.LogInfo
import xyz.labmem.util.getResourceFile

class MainStage : BaseStage() {

    companion object {
        var self: MainStage? = null
        private val finishShowToDo = ArrayList<() -> Unit>()
        fun setCursor(cursor: Cursor?) {
            self?.scene?.cursor = cursor
        }

        fun cursorBack() {
            self?.scene?.cursor = mouseImage["m1"]
        }

        fun loadingView(run: () -> Unit) {
            self?.loading?.play(run)
        }

        fun formDialog(form: FormInterFace) {
            self?.formDialog?.run(form)
        }

        fun confirmDialog(title: String, label: String, cancel: (() -> Unit)? = null, confirm: () -> Boolean) {
            self?.confirmDialog?.run(title, label, confirm, cancel)
        }

        fun onFinishShow(f: () -> Unit) = finishShowToDo.add(f)
    }

    private val loading: LoadingStage
    private val formDialog: FormDialog
    private val confirmDialog: ConfirmDialog
    val td = TopDrag(this)
    val cp = CenterPane(this)

    init {
        self = this
        formDialog = FormDialog(this)
        confirmDialog = ConfirmDialog(this)
        loading = LoadingStage(this)
    }

    fun run() {
        title = AppConfig.appName
        icons.add(Image(getResourceFile(AppConfig.appIcon).stream))
        root.setPrefSize(ScreenType.getWidth(), ScreenType.getHeight())
        root.background = Background.EMPTY
//        root.style = "-fx-border-color: red; -fx-border-width: 1px;"
        scene.fill = null
        initStyle(StageStyle.TRANSPARENT)
        scene.cursor = mouseImage["m1"]
        scene.stylesheets.add("/css/scrollPane.css")
        scene.stylesheets.add("/css/table.css")
        renovation()
        show()
        BackgroundMusic.played()
        toFront()
        AppTray(this)
        finishShowToDo.forEach { f -> f() }
        LogInfo.appendLog("欢迎使用 LIPX-P5R 客户端 !")
        Thread {
            //服务自动启动
            if (AppContext.Server.cacheData!!.autoRun) {
                AppContext.Server.server.start(AppContext.Server.cacheData!!)
            }
            //客户端自动连接
            ClientAutoRunTask.start()
        }.start()
    }

    private fun renovation() {
        root.children.add(td)
        root.children.add(Pane(cp).apply {
            style = """
            -fx-background-color: RGB(255, 255, 255);
            -fx-border-color: rgb(0,0,0); 
            -fx-border-width: 5px;
        """.trimIndent()
            layoutYProperty().bind(td.heightProperty())
        })

    }

}